//
//  PickerImageView.m
//  相机
//
//  Created by 张磊 on 14-11-11.
//  Copyright (c) 2014年 com.zixue101.www. All rights reserved.
//

#import "ZLPhotoPickerImageView.h"
#import "UIImage+ZLPhotoLib.h"

#import <AVFoundation/AVFoundation.h>
@interface ZLPhotoPickerImageView ()

@property (nonatomic , weak) UIView *maskView;
@property (nonatomic , weak) UIButton *tickImageView;
@property (nonatomic , weak) UIImageView *videoView;
@property (nonatomic , weak) UILabel *timeLabel;

@end

@implementation ZLPhotoPickerImageView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.contentMode = UIViewContentModeScaleAspectFill;
        self.clipsToBounds = YES;
    }
    return self;
}

- (UIView *)maskView{
    if (!_maskView) {
        UIView *maskView = [[UIView alloc] init];
        maskView.frame = self.bounds;
        maskView.backgroundColor = [UIColor whiteColor];
        maskView.alpha = 0.5;
        [self addSubview:maskView];
        self.maskView = maskView;
    }
    return _maskView;
}

#warning - 改了这里增加了一个timeLabel
-(UILabel *)timeLabel{
    
    if (!_timeLabel) {
        UILabel *timeLabel = [[UILabel alloc] init];
        CGFloat interval = 10;
        CGFloat w = 50;
        CGFloat h = 30;
        CGFloat y = self.bounds.size.height - 30 - interval;
        CGFloat x = self.bounds.size.width - 50 - interval;
        timeLabel.frame = CGRectMake(x, y, w, h);
        timeLabel.font = [UIFont systemFontOfSize:13];
        timeLabel.textColor = [UIColor whiteColor];
        timeLabel.textAlignment = NSTextAlignmentRight;
        
        
        [self addSubview:timeLabel];
        self.timeLabel = timeLabel;
    }
    
    return _timeLabel;
}

- (UIImageView *)videoView{
    if (!_videoView) {
        UIImageView *videoView = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.bounds.size.height - 40, 30, 30)];
#warning - 这里我把视频图片去掉了，要视频的话，这个填的字符串名字是@“video”
        videoView.image = [UIImage ml_imageFromBundleNamed:@"video"];
        videoView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:videoView];
        self.videoView = videoView;
    }
    return _videoView;
}

- (UIButton *)tickImageView{
    if (!_tickImageView) {
        UIButton *tickImageView = [[UIButton alloc] init];
        tickImageView.frame = CGRectMake(self.bounds.size.width - 28, 5, 21, 21);
//        [tickImageView addTarget:self action:@selector(clickTick) forControlEvents:UIControlEventTouchUpInside];
        [tickImageView setImage:[UIImage ml_imageFromBundleNamed:@"icon_image_no"] forState:UIControlStateNormal];
        [self addSubview:tickImageView];
        self.tickImageView = tickImageView;
    }
    return _tickImageView;
}

- (void)setIsVideoType:(BOOL)isVideoType{
    _isVideoType = isVideoType;
    
    self.videoView.hidden = !(isVideoType);
    
    self.timeLabel.hidden = !(isVideoType);
}


#warning - 添加了timeLabel视频的时长
-(void)setAssetURL:(NSURL *)assetURL{
    
    NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
    AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:assetURL options:opts];
    // 初始化视频媒体文件
    NSInteger minute = 0;
    NSInteger second = 0;
    second = urlAsset.duration.value / urlAsset.duration.timescale;
    if (second >= 60) {
        NSInteger index = second / 60;
        minute = index;
        second = second - index*60;
    }
    
    NSString *minStr = [NSString stringWithFormat:@"%ld",minute];
    NSString *secondStr = [NSString stringWithFormat:@"%ld",second];
    
    if (minStr.length == 1) {
        minStr = [NSString stringWithFormat:@"0%@",minStr];
    }
    
    if (secondStr.length == 1) {
        secondStr = [NSString stringWithFormat:@"0%@",secondStr];
    }
    
    self.timeLabel.text = [NSString stringWithFormat:@"%@:%@",minStr,secondStr];
}

- (void)setMaskViewFlag:(BOOL)maskViewFlag{
    _maskViewFlag = maskViewFlag;
    
    //    self.maskView.hidden = !maskViewFlag;
    self.animationRightTick = maskViewFlag;
}

- (void)setMaskViewColor:(UIColor *)maskViewColor{
    _maskViewColor = maskViewColor;
    
    self.maskView.backgroundColor = maskViewColor;
}

- (void)setMaskViewAlpha:(CGFloat)maskViewAlpha{
    _maskViewAlpha = maskViewAlpha;
    
    self.maskView.alpha = maskViewAlpha;
}

- (void)setAnimationRightTick:(BOOL)animationRightTick{
    _animationRightTick = animationRightTick;

    if (animationRightTick) {
        [self.tickImageView setImage:[UIImage ml_imageFromBundleNamed:@"icon_image_yes"] forState:UIControlStateNormal];
    }else{
        [self.tickImageView setImage:[UIImage ml_imageFromBundleNamed:@"icon_image_no"] forState:UIControlStateNormal];
    }
    
    if (!self.isClickHaveAnimation) {
        return;
    }
    CAKeyframeAnimation *scaoleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    scaoleAnimation.duration = 0.25;
    scaoleAnimation.autoreverses = YES;
    scaoleAnimation.values = @[[NSNumber numberWithFloat:1.0],[NSNumber numberWithFloat:1.2],[NSNumber numberWithFloat:1.0]];
    scaoleAnimation.fillMode = kCAFillModeForwards;
    
    if (self.isVideoType) {
        [self.videoView.layer removeAllAnimations];
        [self.videoView.layer addAnimation:scaoleAnimation forKey:@"transform.rotate"];
    }else{
        [self.tickImageView.layer removeAllAnimations];
        [self.tickImageView.layer addAnimation:scaoleAnimation forKey:@"transform.rotate"];
    }
}
@end
